package com.hyl.domain.activity.service.trial.node;

import com.hyl.domain.activity.model.entity.MarketProductEntity;
import com.hyl.domain.activity.model.entity.TrialBalanceEntity;
import com.hyl.domain.activity.service.trial.AbstractGroupBuyMarketSupport;
import com.hyl.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import com.hyl.types.design.framework.tree.StrategyHandler;
import com.hyl.types.enums.ResponseCode;
import com.hyl.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author hyl
 * @description
 * @create 2025-06-15 16:07
 */
@Service
@Slf4j
public class SwitchRoot extends AbstractGroupBuyMarketSupport<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> {
    @Resource
    private MarketNode marketNode;
    @Override
    public TrialBalanceEntity doApply(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {

        String userId = requestParameter.getUserId();
        if (repository.downgrandSwitch()){
            log.info("拼团活动降级拦截 {}", userId);
            throw new AppException(ResponseCode.E0003.getCode(), ResponseCode.E0003.getInfo());
        }
        if (!repository.cutRange(userId)){
            log.info("拼团活动切量拦截 {}", userId);
            throw new AppException(ResponseCode.E0004.getCode(), ResponseCode.E0004.getInfo());
        }


        return router(requestParameter,dynamicContext);
    }
    @Override
    public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        return marketNode;
    }
}
